package com.css.fxfzaqbz.modules.callJcData.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.modules.callJcData.entity.Gasholder;

import com.css.fxfzaqbz.modules.callJcData.param.GasholderQueryParams;
import com.css.fxfzaqbz.modules.callJcData.service.GasholderService;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import cn.hutool.core.io.FileUtil;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 次生灾害危险源数据子模块 储气罐数据    //重点场所数据为 SiteController
 */
@RestController
@RequestMapping("/fxfzaqbz/gasholder")
@Slf4j
public class GasholderController {

    @Resource
    GasholderService gasholderService;

    //获取配置文件的上传文件路径
    @Value("${file.upload.path}")
    private String uploadPath;

    /**
     * 新增数据
     * @param gasholder
     * @return
     */
    @PostMapping("save")
    public RestResponse save(@RequestBody Gasholder gasholder) {
        try {

            gasholderService.save(gasholder);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error("新增数据失败!", e);
            return RestResponse.fail("新增数据失败!");
        }
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("findById/{id}")
    public RestResponse findById(@PathVariable("id") String id) {
        try {
            return RestResponse.succeed(gasholderService.findById(id));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 根据id修改数据
     *
     * @param param
     * @return
     */
    @PostMapping("updateById")
    public RestResponse updateById(@RequestBody Gasholder param) {
        try {
            String userID = PlatformSessionContext.getUserID();
            param.setUpdateUser(userID);
            gasholderService.updateById(param);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error("修改数据失败!", e);
            return RestResponse.fail("修改数据失败!");
        }
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return
     */
    @GetMapping("deleteById/{id}")
    public RestResponse deleteById(@PathVariable("id") String id) {
        try {
            gasholderService.deleteById(id);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error("删除数据失败!", e);
            return RestResponse.fail("删除数据失败!");
        }
    }

    /**
     * 分页条件查询
     *
     * @param param
     * @return
     */
    @GetMapping("page")
    public RestResponse page(GasholderQueryParams param) {
        try {
            return RestResponse.succeed(gasholderService.page(param));
        } catch (Exception e) {
            log.error("查询数据失败!", e);
            return RestResponse.fail("查询数据失败!");
        }
    }

    /**
     * 数据导入功能，目前是支持excel数据的导入
     *
     * @param file
     * @return
     */
    @PostMapping("dataImport")
    public RestResponse dataImport(@RequestParam MultipartFile file) {
        try {
            if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
                return RestResponse.fail("文件格式不支持!");
            }
            gasholderService.dataImport(file.getInputStream());
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail("业务繁忙");
        }
    }

    /**
     * 数据导出，将符合条件的数据导出为excel07-xlsx格式
     *
     * @param response
     * @param idList：要导出的数据的id传，根据,进行分隔成列表
     */
    @GetMapping("dataExport")
    public void dataExport(HttpServletResponse response, @RequestParam String idList) {
        try {
            gasholderService.exportExcel(response);
            RestResponse.succeed("导出成功");
        } catch (IOException e) {
            log.error("导出失败!",e);
            RestResponse.fail("导出失败!");
        }
    }
}
